package com.resrun.utils;

import com.resrun.exception.CaException;

import java.security.InvalidParameterException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

public final class KeyUtil {

    private static final String ALGORITHM = "RSA";
    private static final int KEY_SIZE_1024 = 1024;
    private static final int KEY_SIZE_2048 = 2048;
    private static final int DEFAULT_KEY_SIZE = KEY_SIZE_2048;

    private KeyUtil() {
        throw new UnsupportedOperationException("工具类不允许实例化");
    }

    public static KeyPair generateKeyPair() {
        return generateKeyPair(DEFAULT_KEY_SIZE);
    }

    public static KeyPair generateKeyPair(final int keySize) {
        try {
            final KeyPairGenerator gen = KeyPairGenerator.getInstance(ALGORITHM);
            gen.initialize(keySize);
            return gen.generateKeyPair();
        } catch (final NoSuchAlgorithmException | InvalidParameterException e) {
            throw new CaException(e);
        }
    }
}
